package cn.micro.system.manage.annotate.impl;


import cn.micro.system.manage.annotate.UniqueDept;
import cn.micro.system.manage.annotate.UniqueEmployee;
import cn.micro.system.manage.entity.SysDimDept;
import cn.micro.system.manage.entity.SysDimEmployee;
import cn.micro.system.manage.service.SysDimDeptService;
import cn.micro.system.manage.service.SysDimEmployeeService;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.function.Predicate;

public class EmployeeValidation <T extends Annotation> implements ConstraintValidator<T, SysDimEmployee>  {

    protected Predicate<SysDimEmployee> predicate = c -> true;

    @Resource
    protected SysDimEmployeeService sysDimEmployeeService;

    @Override
    public boolean isValid(SysDimEmployee sysDimEmployee, ConstraintValidatorContext constraintValidatorContext) {
        return sysDimEmployeeService == null || predicate.test(sysDimEmployee);
    }

    /**
     * 校验部门编码或者部门名称是否重复
     */
    public static class UniqueEmployeeValidate extends EmployeeValidation<UniqueEmployee>{
        @Override
        public void initialize(UniqueEmployee constraintAnnotation) {
            predicate = c -> sysDimEmployeeService.getCode(c).isEmpty();
        }
    }
}
